<?php
/**
 * Created by PhpStorm.
 * User: xsp
 * Date: 2018/4/20
 * Time: 16:20
 */

namespace common\models;


use backend\models\Profile;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;

class User extends ActiveRecord implements IdentityInterface
{
    public function rules()
    {
        return [
            ['username', 'required', 'message' => '用户名不能为空','on'=>['back','front']],
            ['userpass', 'required', 'message' => '用户密码不能为空','on'=>['back','front']],
            ['useremail', 'required', 'message' => '用户邮箱不能为空','on'=>'back'],
            ['checkpass', 'required', 'message' => '确认密码不能为空','on'=>'back'],
            ['checkpass', 'compare', 'compareAttribute' => 'userpass', 'message' => '两次密码不一致','on'=>'back'],
            ['username', 'unique', 'targetClass' => 'backend\models\User', 'message' => '该用户名已经被占用','on'=>'back'],
            ['useremail', 'unique', 'targetClass' => 'backend\models\User', 'message' => '该邮箱已经被占用','on'=>'back'],
            ['userpass','validatePass','message'=>'用户名或密码错误']
        ];
    }

    public $checkpass;
    public $rememberMe;

    public static function findIdentity($id){
        return static::findOne($id);
    }
    public static function findIdentityByAccessToken($token, $type = null){
        return null;
    }
    public function getId()
    {
        return $this->userid;
    }
    public function getAuthKey(){
        return null;
    }
    public function validateAuthKey($authKey){
        return true;
    }

    public function getProfile()
    {
        return $this->hasOne(Profile::className(), ['userid' => 'userid']);
    }

    public function AddUser($data)
    {
        $this->scenario='back';
        if($this->load($data)&&$this->validate())
        {
            $this->userpass = md5($this->userpass);
            $this->createtime = time();
            $pro = new Profile();
            $pro->userid = $this->userid;
            $pro->truename = "未填写";
            $pro->nickname = $this->username;
            $pro->company = "未填写";
            $tran = \Yii::$app->db->beginTransaction();
            try{
            if($this->save(false))
            {
                $pro->userid = $this->attributes['userid'];

                if (!$pro->save(false))
                {
                    throw new \Exception();
                }
            }
            $tran->commit();
            }catch (\Exception $e)
            {
                $tran->rollBack();
                return false;
            }
            return true;

        }
    }
    public function validatePass()
    {
        if(!$this->hasErrors())
        {

            $data = self::find()->where(['username'=>$this->username,'userpass'=>md5($this->userpass)])->one();

            if (is_null($data)) {
                $this->addError('userpass','用户名或密码错误');
            }
        }
    }
    public function getUser()
    {
        return self::find()->where(['username'=>$this->username])->one();
    }

    public function login($data)
    {
        $this->scenario = "front";

        if($this->load($data)&&$this->validate())
        {
            return \Yii::$app->user->login($this->getUser(),$this->rememberMe?3600*24:0);
        }
        return false;
       /* {
            $user = self::find()->select('userid')->where(['username'=>$this->username])->one();

            $session = \Yii::$app->session;
//            session_set_cookie_params(0);
            $session['user']=[
                'uid'=>$user->userid,
                'user'=>$this->username,
                'islogin'=>true,
            ];
            return $session['user']['islogin'];
        } */




    }

}